/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * It would probably be a good idea to move place and remove out to its own interface
 */
package game.platformer.interfaces;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import game.platformer.combat.BulletPool;
import game.platformer.platformer;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.extension.physics.box2d.util.constants.PhysicsConstants;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
/**
 *
 * @author cow
 */
public class CharRepresentation {
	//varblock---------------
	//representation data----
	private Body phyRep;
	private Sprite charRep;
	private PhysicsConnector phyCon;
	//position data----------
	private int maxTraversRate = 10;
	private int maxJumpRate = 10;
	private boolean placed;
	//unit data--------------
	private int health;
	private int maxHealth;
	private float weaponCooldown;
	private float weaponRefireTime;
	//-----------------------
	//Bullet data-------------
	private BulletPool mBulletPool;
	//constructor---------------------------
	public CharRepresentation(Sprite spr, Body bdy){
		charRep = spr;
		phyRep = bdy;
		phyCon = new PhysicsConnector(spr, bdy, true, false);
		placed = false;

		maxHealth = 4;
		health = 4;

		weaponCooldown = 0;
		weaponRefireTime = 2;

		charRep.registerUpdateHandler(new IUpdateHandler() {
			public void onUpdate(float pSecondsElapsed){
				if(weaponCooldown > 0){
					weaponCooldown -= pSecondsElapsed;
				}
			}
			public void reset(){
			}
		});
	}
	//position data-------------------------
	public void place(int x, int y){
		if(!placed){
			platformer.getScreenManager().getMapScreen().attachChild(charRep);
			platformer.getPhysicsWorld().registerPhysicsConnector(phyCon);
			placed = true;
		}
		phyRep.setTransform(new Vector2(x, y).mul(1 / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT), 0);
		phyRep.setLinearVelocity(0, 0);
	}
	public void remove(){
		if(placed){
			platformer.getScreenManager().getMapScreen().detachChild(charRep);
			platformer.getPhysicsWorld().unregisterPhysicsConnector(phyCon);
			placed = false;
		}
	}
	public void kill(){
		remove();
		phyRep.setActive(false);
	}
	//commands------------------------------
	public void traverse(float percentOfMax){
		if(percentOfMax > 1){
			percentOfMax = 1;
		}
		phyRep.setLinearVelocity(percentOfMax * maxTraversRate, phyRep.getLinearVelocity().y);
	}
	public void jump(float percentOfMax){
		if(percentOfMax > 1){
			percentOfMax = 1;
		}
		if(phyRep.getLinearVelocity().y > -0.25 && phyRep.getLinearVelocity().y < 0.25){
			phyRep.setLinearVelocity(phyRep.getLinearVelocity().x, maxJumpRate * percentOfMax);
		}
	}
	//Bullet Functions----------------------
	public void initBullet(Scene mapScreen, TextureRegion bulletTR, PhysicsWorld phyW){
		mBulletPool = new BulletPool(mapScreen, bulletTR, phyW);
	}
	public void shoot(Vector2 direction){
		if(weaponCooldown <= 0){
			mBulletPool.launch(charRep.getX() + 10,
				charRep.getY() + 10, direction);
			weaponCooldown = weaponRefireTime;
		}
	}
	//representations-----------------------
	public Sprite getSprite(){
		return charRep;
	}
	public Body getBody(){
		return phyRep;
	}
	public PhysicsConnector getPhyCon(){
		return phyCon;
	}
	public void setSprite(Sprite s){
		remove();
		charRep = s;
		phyRep.createFixture(PhysicsFactory.createFixtureDef(1, 0, 0));
		phyCon = new PhysicsConnector(charRep, phyRep);
	}
	//health data---------------------------
	public int getHealth(){
		return health;
	}
	public void setHealth(int health){
		this.health = health;
	}
	public void setMaxHealth(int health){
		maxHealth = health;
	}
	public int hurt(int damage){
		health -= damage;
		if(health < 0){
			remove();
		}
		return health;
	}
	public int heal(int healing){
		if(health > 0){
			health += healing;
			if(health > maxHealth){
				health = maxHealth;
			}
		}
		return health;
	}
}
